<template>
  <div class="app-container">
    <el-form :inline="true">
      <el-form-item>
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="showInsert"
        >{{ $t('insert') }}
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="companyMonitorList"
      style="width: 100%"
    >
      <el-table-column :label="$t('icon')" width="50" >
        <template slot-scope="scope">
          <el-image
            style="width: 40px;height: 40px;margin-top: 10px"
            :src="scope.row.iconUrl"
            fit="fit"
          >
            <div slot="error" class="image-slot">
              <i style="font-size: 35px" class="el-icon-picture-outline" />
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        :label="$t('name')"
      />
      <el-table-column
        prop="url"
        :label="$t('url')"
      />
      <el-table-column
        prop="sort"
        :label="$t('sort')"
      />
      <el-table-column prop="status" :label="$t('status')" sortable  width="80">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.status?'success':'danger'"
            @click="changeStatus(scope.row)"
            v-text="scope.row.status ? $t('enable'):$t('DISABLE')"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('time')"
      >
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span style="margin-left: 13px">{{
            formatDate(scope.row.createdDate)
          }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('function')" width="150">
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-edit"
            @click="edit(scope.row)"
          >{{ $t('edit') }}
          </el-button>
          <el-button
            style="color: #f56c6c"
            type="text"
            icon="el-icon-delete"
            @click="del(scope.row)"
          >{{ $t('delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :visible.sync="dialogCompanyMonitorVisible"
      top="15px"
    >
      <el-form
        label-width="20%"
        :model="companyMonitor"
      >
        <el-form-item :label="$t('name')">
          <el-input v-model="companyMonitor.name" />
        </el-form-item>
        <el-form-item :label="$t('icon')">
          <el-input v-model="companyMonitor.iconUrl" />
        </el-form-item>
        <el-form-item :label="$t('url')">
          <el-input v-model="companyMonitor.url" />
        </el-form-item>
        <el-form-item :label="$t('sort')">
          <el-input v-model="companyMonitor.sort" />
        </el-form-item>
        <el-form-item :label="$t('status')">
          <el-switch
            v-model="companyMonitor.status"
            :active-text="$t('open')"
            :inactive-text="$t('close')"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogCompanyMonitorVisible = false">{{ $t('cancel') }}</el-button>
        <el-button type="primary" @click="addOrUpdate">{{ $t('confirm') }}</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import {
  formatDate,
  successNotice,
  cancelNotice
} from '@/util/common'
import {
  listCompanyMonitor,
  updateCompanyMonitor,
  deleteCompanyMonitor,
  insertCompanyMonitor
} from '@/api/iot/iotCompanyMonitorApi'
import { getCompanyId } from '@/util/storage'

export default {
  name: 'CompanyMonitor',
  data() {
    return {
      companyMonitorList: [],
      companyMonitor: {},
      dialogCompanyMonitorVisible: false,
      companyId: getCompanyId()
    }
  }, created() {
    this.listCompanyMonitor()
  },
  methods: {
    changeStatus(item) {
      const data = {
        id: item.id,
        status: !item.status
      }
      updateCompanyMonitor(data).then(res => {
        if (res.code === '200') {
          successNotice(this)
        }
        this.listCompanyMonitor()
      })
    },
    formatDate(time) {
      return formatDate(this, time)
    },
    showInsert() {
      this.companyMonitor = {}
      this.dialogCompanyMonitorVisible = true
    },
    listCompanyMonitor() {
      listCompanyMonitor({}).then(res => {
        const { code, data } = res
        if (code === '200') {
          this.companyMonitorList = data
        } else {
          this.companyMonitorList = []
        }
      })
    },
    addOrUpdate() {
      const json = {
        ...this.companyMonitor
      }
      if (!json.id) {
        json.companyId = this.companyId
        insertCompanyMonitor(json).then(res => {
          const { code } = res
          if (code === '200') {
            this.dialogCompanyMonitorVisible = false
          }
          this.listCompanyMonitor()
        })
      } else {
        updateCompanyMonitor(json).then(res => {
          const { code } = res
          if (code === '200') {
            this.dialogCompanyMonitorVisible = false
          }
          this.listCompanyMonitor()
        })
      }
    },
    edit(item) {
      this.companyMonitor = item
      this.dialogCompanyMonitorVisible = true
    },

    del(item) {
      const that = this
      this.$confirm(this.$t('delete') + item.name, this.$t('prompt'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      })
        .then(function() {
          deleteCompanyMonitor({
            id: item.id
          }).then(res => {
            const { code } = res
            if (code === '200') {
              successNotice(that)
            }
            that.listCompanyMonitor()
          })
        })
        .catch(function() {
          cancelNotice(that)
        })
    }
  }
}
</script>


